
-- Compute depth from pressure and latitude
-- assumes 32 psu
CREATE OR REPLACE FUNCTION oceandepth(real, real)
  RETURNS real AS
$BODY$DECLARE 
  pressure ALIAS FOR $1;
  latitude ALIAS FOR $2;
  x float4;
  g float4;
BEGIN
  x := (sin(latitude / 57.29578))^2;
  g := 9.780318 * (1.0 + ( 0.0052788  + 0.0000236  * x) * x ) + 0.000001092*pressure;
  RETURN ((((-1.82*(1/10^15)  * pressure + 2.279*(1/10^10) ) * pressure - 0.000022512 ) * pressure + 9.72659) * pressure) / g;
END$BODY$
  LANGUAGE 'plpgsql' IMMUTABLE;
COMMENT ON FUNCTION oceandepth(real, real) IS 'depth from pressure and latitude, from Seabird calculation at http://www.seabird.com/application_notes/AN69.htm';